{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "adaptive-tragedy",
   "metadata": {},
   "source": [
    "# Finnish Association Registry IDs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "decimal-updating",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "shaped-squad",
   "metadata": {},
   "source": [
    "The function `clean_fi_associationid()` cleans a column containing Finnish association registry id (associationid) strings, and standardizes them in a given format. The function `validate_fi_associationid()` validates either a single associationid strings, a column of associationid strings or a DataFrame of associationid strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "funky-legislation",
   "metadata": {},
   "source": [
    "associationid strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"1234\"\n",
    "* `standard`: associationid strings with proper whitespace in the proper places, like \"1.234\"\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_fi_associationid()` and `validate_fi_associationid()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "juvenile-manchester",
   "metadata": {},
   "source": [
    "### An example dataset containing Finnish association registry id strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "royal-pasta",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"associationid\": [\n",
    "            \"1234\",\n",
    "            \"12df\",\n",
    "            \"51824753556\",\n",
    "            \"51 824 753 556\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\"\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "governing-fruit",
   "metadata": {},
   "source": [
    "## 1. Default `clean_fi_associationid`\n",
    "\n",
    "By default, `clean_fi_associationid` will clean Finnish association registry id strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "brazilian-thread",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_fi_associationid\n",
    "clean_fi_associationid(df, column = \"associationid\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "amateur-talent",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "indian-child",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "involved-issue",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "suffering-swift",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fi_associationid(df, column = \"associationid\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lesbian-chance",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "industrial-gazette",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fi_associationid(df, column = \"associationid\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adult-journalist",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned Finnish association registry id strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "attractive-astronomy",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fi_associationid(df, column=\"associationid\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "chemical-french",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "virtual-friendly",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "egyptian-brighton",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fi_associationid(df, \"associationid\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sustained-salem",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "vertical-blues",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fi_associationid(df, \"associationid\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "disturbed-spirituality",
   "metadata": {},
   "source": [
    "## 4. `validate_fi_associationid()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "embedded-pacific",
   "metadata": {},
   "source": [
    "`validate_fi_associationid()` returns `True` when the input is a valid Finnish association registry id. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_fi_associationid()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_fi_associationid()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_fi_associationid()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sitting-marks",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_fi_associationid\n",
    "print(validate_fi_associationid(\"1234\"))\n",
    "print(validate_fi_associationid(\"12df\"))\n",
    "print(validate_fi_associationid(\"51824753556\"))\n",
    "print(validate_fi_associationid(\"51 824 753 556\"))\n",
    "print(validate_fi_associationid(\"hello\"))\n",
    "print(validate_fi_associationid(np.nan))\n",
    "print(validate_fi_associationid(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "julian-courtesy",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "covered-manor",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_fi_associationid(df[\"associationid\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "public-herald",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "written-sellers",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_fi_associationid(df, column=\"associationid\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "racial-offer",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "willing-broadcasting",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_fi_associationid(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "baking-oxide",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
